<template>
	<view class="container">
		<!-- 小程序头部兼容 -->
		<!-- #ifdef MP -->
		<view class="mp-search-box">
			<input @click="searchInput" class="ser-input" type="text" value="输入关键字搜索" disabled />
		</view>
		<!-- #endif -->
		<!-- 未登录状态底部显示 -->
		<template v-if="!hasLogin">
			<view class="notLogin">
				<view class="leftText">登录后更多精彩…………</view>
				<button type="primary" class="rightBtn" size="mini" @click="login">点击登录</button>
			</view>
		</template>
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 标题栏和状态栏占位符 -->
			<view class="titleNview-placing"></view>
			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
			<swiper class="carousel" circular @change="swiperChange">
				<swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item" @click="navToDetailPage({title: '轮播广告'})">
					<image :src="item.src" />
				</swiper-item>
			</swiper>
			<!-- 自定义swiper指示器 -->
			<view class="swiper-dots">
				<text class="num">{{swiperCurrent+1}}</text>
				<text class="sign">/</text>
				<text class="num">{{swiperLength}}</text>
			</view>
		</view>


		<!-- 猜你喜欢 -->
		<view class="f-header m-t">
			<image src="/static/temp/h1.png"></image>
			<view class="tit-box">
				<text class="tit">今日推荐</text>
				<text class="tit2">About Us</text>
			</view>
		</view>

		<!-- 首页列表 -->
		<view class="guess-section">
			<block v-for="(item, index) in goodsList" :key="index">
				<view class="guess-item" @click="navToDetailPage(item)">
					<view class="image-wrapper">
						<image :src="item.primary_img" mode="aspectFill" lazy-load></image>
					</view>
					<text class="title clamp">{{item.product_name}}</text>
					<text class="price">￥{{item.sale_price}}</text>
				</view>
			</block>
		</view>

		<uni-load-more v-if="goodsList.length > 0" :status="loadMore"></uni-load-more>
	</view>
</template>

<script>
	import {
		getProductList
	} from "@/common/request.js"
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				loadMore: "more",
				titleNViewBackground: '',
				swiperCurrent: 0,
				swiperLength: 0,
				carouselList: [],
				goodsList: []
			};
		},
		computed: {
			...mapState(['hasLogin'])
		},
		onLoad() {
			this.loadData();
			this.getGoodsLists();
		},
		onReachBottom() {
			this.getGoodsLists();
		},
		onPullDownRefresh() {
			this.getGoodsLists();
		},
		methods: {
			login() {
				uni.switchTab({
					url: "../user/user"
				})
			},
			//获取商品列表
			async getGoodsLists() {
				if (this.loadMore === "loading") {
					return;
				}

				this.loadMore = "loading";

				//商品列表
				let goods = await getProductList({isHome:true});
				if (goods.code === 0) {
					uni.stopPullDownRefresh();
					let newGoods = goods.data;
					if (newGoods.length <= 0) {
						this.loadMore = "noMore"
					} else {
						if (this.goodsList.length <= 0) {
							this.goodsList = newGoods || [];
						} else {
							newGoods.forEach(item => {
								this.goodsList.push(item);
							})
						}
						this.loadMore = "more"
					}
				}
				uni.stopPullDownRefresh();
			},
			async loadData() {
				//轮播图数据
				let carouselList = await this.$api.json('carouselList');
				this.titleNViewBackground = carouselList[0].background;
				this.swiperLength = carouselList.length;
				this.carouselList = carouselList;

			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
			//详情页
			navToDetailPage(item) {
				let id = item._id;
				uni.navigateTo({
					url: `/pages/product/product?id=${id}`
				})
			},
			//搜索 框
			searchInput() {
				uni.navigateTo({
					url: "../search/search"
				})
			}
		}
	}
</script>

<style lang="scss">
	.notLogin {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 10rpx 20rpx;
		background: #FFFFFF;
		box-shadow: 0 0 12rpx #C0C0C0;
		position: fixed;
		bottom: 20rpx;
		left: 20rpx;
		right: 20rpx;
		border-radius: 50rpx;

		.leftText {
			font-size: 28rpx;
			flex: 1;
			font-size: 28rpx;
		}

		.rightBtn {
			height: 80rpx;
			border-radius: 50rpx;
			background: #fa6c9f;
			background: linear-gradient(to right, #fa6c9f 0%, #ffe140 80%, #ffe140 100%);
		}
	}

	.rightBtn::after {
		border: none;
	}

	/* #ifdef MP */
	.mp-search-box {
		position: absolute;
		left: 0;
		top: 30upx;
		z-index: 9999;
		width: 100%;
		padding: 0 80upx;

		.ser-input {
			flex: 1;
			height: 56upx;
			line-height: 56upx;
			text-align: center;
			font-size: 28upx;
			color: $font-color-base;
			border-radius: 20px;
			background: rgba(255, 255, 255, .6);
		}
	}

	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
			margin-top: -20upx;
		}

		.carousel-section {
			padding: 0;

			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}

			.carousel {
				.carousel-item {
					padding: 0;
				}
			}

			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}

	/* #endif */


	page {
		background: #f5f5f5;
	}

	.m-t {
		margin-top: 16upx;
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		// padding-top: 10px;

		.titleNview-placing {
			height: var(--status-bar-height);
			// padding-top: 44px;
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 300rpx;
			transition: .4s;
		}
	}

	.carousel {
		width: 100%;
		height: 350upx;

		.carousel-item {
			width: 100%;
			height: 100%;
			padding: 0 28upx;
			overflow: hidden;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;

		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}

		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}

	//猜你喜欢标题
	.f-header {
		display: flex;
		align-items: center;
		height: 140upx;
		padding: 6upx 30upx 8upx;
		background: #fff;

		image {
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			margin-right: 20upx;
		}

		.tit-box {
			flex: 1;
			display: flex;
			flex-direction: column;
		}

		.tit {
			font-size: $font-lg +2upx;
			color: #font-color-dark;
			line-height: 1.3;
		}

		.tit2 {
			font-size: $font-sm;
			color: $font-color-light;
		}

		.icon-you {
			font-size: $font-lg +2upx;
			color: $font-color-light;
		}
	}

	.guess-section {
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		background: #fff;

		.guess-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			font-size: $font-lg;
			color: $font-color-dark;
			line-height: 80upx;
		}

		.price {
			font-size: $font-lg;
			color: $uni-color-primary;
			line-height: 1;
		}
	}
</style>
